package util;

import java.io.ByteArrayInputStream;
import org.codehaus.jettison.mapped.Configuration;
import org.codehaus.jettison.mapped.MappedDOMDocumentParser;
import org.dom4j.Document;

/**
 * A util box handling json
 * 
 * @author tanjian
 * 
 */
public class JsonUtil {

	/**
	 * Using Jettison to transform Json to Dom4j DOM
	 * 
	 * @param json
	 * @return
	 * @throws Exception
	 */
	public static Document toDOM(String json) throws Exception {
		return DOMUtil.parse(new MappedDOMDocumentParser(new Configuration())
				.parse(new ByteArrayInputStream(json.getBytes())));
	}

	public static void main(String[] args) throws Exception {
		Document document = toDOM("{\"alice\": {\"bob\": \"a\"}}");
		// ItemUtil.printDOM(document);

		System.out.println(ItemUtil.setElements(DOMUtil.toItemList(document)));
	}
}
